﻿using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CourseUtils;

// http://www.platesh.ru/annuitetnie-plateshi/

namespace creditCalculator
{
   

    class Program
    {
        static void inputData(out int creditSum, out int rate, out int timePeriod)
        {
            creditSum = CourseUtils.IOHelpers.ReadIntFromConsole("Input credit sum in UAN");
            rate = CourseUtils.IOHelpers.ReadIntFromConsole("Input rate in %");
            timePeriod = CourseUtils.IOHelpers.ReadIntFromConsole("Input time period in months");
            return;
        }
        static double calculateAnnuityMonthlyPayment(int creditSum, int rate, int timePeriod)
        {
            double monthlyRate = (double)rate * 0.01 / 12.0;
            double intermediatePart = (double)Math.Pow(1.0 + monthlyRate, timePeriod);
            double monthlyPayment = creditSum * (monthlyRate + monthlyRate / (intermediatePart - 1));
            return monthlyPayment;
        }
        static double calculatePercentagePartOfAnnuityPayment(double DebtBalanceForPeriod, int rate)
        {
            double accruedInterest = DebtBalanceForPeriod * rate * 0.01 / 12;
            return accruedInterest;
        }
        static double calculatePartForPayDownDebt(double oneMonthPayment, double accruedInterest)
        {
            double PartForPayDownDebt = oneMonthPayment - accruedInterest;
            return PartForPayDownDebt;
        }

        public static DateTime ReadDateFromConsole(String displayText)
        {
            Console.WriteLine(displayText);

            int year = IOHelpers.ReadIntFromConsole("\nEnter the year:", 1900, 2015);
            int month = IOHelpers.ReadIntFromConsole("Enter the month number:", 1, 12);
            int day = IOHelpers.ReadIntFromConsole("Enter the day:", 1, 31);

            return new DateTime(year, month, day);
        }
        public static DateTime nextCreditPaymentDay(DateTime date, TimeSpan span, int i)
        {
                date = date.AddMonths(i);
       
            if (date.DayOfWeek == DayOfWeek.Saturday)
            {
                TimeSpan notSatuday = new TimeSpan(1, 0, 0, 0);
                date -= notSatuday;
            }
            if (date.DayOfWeek == DayOfWeek.Sunday)
            {
                TimeSpan notSunday = new TimeSpan(2, 0, 0, 0);
                date -= notSunday;
            }
                return date;
            
        }

        static void Main(string[] args)
        {
            // Set the current culture to english
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

            int creditSum;
            int rate;
            int timePeriod;
            inputData(out creditSum, out rate, out timePeriod);
            double monthPayment = calculateAnnuityMonthlyPayment(creditSum, rate, timePeriod);
            int i = 0;
            double DebtBalanceForPeriod = creditSum;
            double currentDebt = creditSum;
            double previesPrincipalDebt = 0;
            DateTime creditDay = ReadDateFromConsole("Please enter date of receiving of the credit");
            TimeSpan delta = new TimeSpan(30, 0, 0, 0);

            Console.Title = "Credit calculator";
            int height = 35;
            int WindowWidth = 150;
            Console.BackgroundColor = ConsoleColor.Cyan;
            Console.ForegroundColor = ConsoleColor.Black;

            Console.WindowHeight = height;
            int WindowTop = Console.WindowTop;
            WindowWidth = Console.WindowWidth;
            Console.Clear();
            Console.WriteLine("|  Number\t|  Payment day\t\t|  Sum\t\t|  Percentages\t|");
            do
            {
                double accruedInterest = calculatePercentagePartOfAnnuityPayment(DebtBalanceForPeriod, rate);
                double principalDebt = calculatePartForPayDownDebt(monthPayment, accruedInterest);
                creditDay = nextCreditPaymentDay(creditDay, delta, i);
                Console.Write("|  {0} month\t", i + 1);

                Console.Write("|  {0:ddd MMM dd, yyyy}\t|", creditDay);
                Console.Write("  {0:#.##} UAN\t|", accruedInterest + monthPayment);  // You should pay on 
                Console.Write("  {0:#.##} UAN\t|", accruedInterest); //Percentages
                if (i != 0)
                {
                    currentDebt = DebtBalanceForPeriod;
                }
                Console.WriteLine("");
                // остаток задолженности на период:
                DebtBalanceForPeriod -= principalDebt;
                previesPrincipalDebt = principalDebt;
                //--------------
                i++;
            } while (i < timePeriod);


            Console.ReadKey();
        }
    }
}
